Fix UE8M0 packing: mask mantissa bits when extracting fp32 exponents#35
Open
yhyang201 wants to merge 1 commit into
Open
Fix UE8M0 packing: mask mantissa bits when extracting fp32 exponents#35yhyang201 wants to merge 1 commit into
yhyang201 wants to merge 1 commit into
Conversation
The transpose_and_pack_fp32_into_ue8m0 and pack_fp32_into_ue8m0 kernels pack 4 fp32 exponent bytes into one uint32 using a shift-and-OR trick that relies on mantissa bits being zero. When the input fp32 scale factors are not exact powers of two (i.e. have non-zero mantissa), mantissa bits leak into adjacent exponent fields, producing garbage values like 0xFF (Inf exponent). This causes NaN output in fp8_einsum when callers pass plain float32 scale factors (e.g. sglang's sglang_per_token_group_quant_fp8), because the kernel internally converts fp32 scales to ue8m0 via these functions. Fix: extract each exponent with (val >> 23) & 0xFF and shift to the correct byte position, extracted into a shared helper function.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Same fix as deepseek-ai#337.
transpose_and_pack_fp32_into_ue8m0andpack_fp32_into_ue8m0pack 4 fp32exponent bytes into one uint32 using shift-and-OR:
When fp32 scale factors have non-zero mantissa (not exact powers of two),
mantissa bits leak into adjacent exponent fields. For example, packing four
copies of 0.006975 (exp=0x77) produces 0x77ffffff instead of 0x77777777.
This causes NaN in
fp8_einsumwhen callers pass plain float32 scales(e.g. sglang's activation quantization for DeepSeek-V4 on Blackwell).
The torch fallback
get_mn_major_tma_aligned_packed_ue8m0_tensor_torchiscorrect (uses
>> 23then.to(kUInt8)which naturally truncates), but theCUDA kernel path doesn't truncate.
Fix: extract each exponent with
(val >> 23) & 0xFFand shift to the correctbyte position, in a shared
pack_4_fp32_exponentshelper.Reproduction